<?php
namespace Home\Controller;
use Think\Controller;
class ReportController extends Controller {

    public function api(){
        $order = I("GET.order");
        switch ($order) {

        case 'day':
            if(I('GET.date')){
                $day = I('GET.date');
                $sql = "SELECT name,count,greater1,greater3,other.yuefang as yuefang,other.yixiang as yixiang
                    FROM total 
                    left join employee on total.eid = employee.eid 
                    left join other on other.eid = total.eid
                    where total.date = '{$day}' group by employee.eid";
                $result['title'] = $day."报表（日报）";
                $result['data']=D()->query($sql);
                header("Access-Control-Allow-Origin:*");
                die(json_encode($result));
            }
            break;
        default:
            $day =date('Y-m-d',strtotime("-1 day"));
            $sql = "SELECT name,count,greater1,greater3,other.yuefang as yuefang ,other.yixiang as yixiang
                FROM total 
                left join employee on total.eid = employee.eid 
                left join other on other.eid = total.eid
                where total.date = '{$day}' group by employee.eid";
            $result['title'] = $day."报表（日报）";
            $result['data']=D()->query($sql);
            header("Access-Control-Allow-Origin:*");
            die(json_encode($result));
            break;
        }
    }

    function getother(){
        $order = I("GET.order");
        switch ($order) {

        case 'day':
            if(I('GET.date')){
                $day = I('GET.date');
                $sql = "SELECT name,yuefang,qianyue
                    FROM other
                    left join employee on other.eid = employee.eid 
                    where other.date = '{$day}' group by employee.eid";
                $result['title'] = $day."报表（日报）";
                $result['data']=D()->query($sql);
                header("Access-Control-Allow-Origin:*");
                die(json_encode($result));
            }
            break;
        default:
            $day =date('Y-m-d',strtotime("-1 day"));
            $sql = "SELECT name,yuefang,yixiang,qianyue
                FROM other
                right join employee on other.eid = employee.eid 
                where other.date='{$day}' group by employee.eid";
            $result['title'] = $day."（日报）";
            $result['data']=D()->query($sql);
            header("Access-Control-Allow-Origin:*");
            die(json_encode($result));
            break;
        }

    }

    function month(){
        if(I('GET.month')){
            $day =date('Y-m',strtotime("-1 day"));
            $sql = "SELECT name,sum(count) as count,sum(greater1) as greater1,sum(greater3) as greater3,sum(yuefang) as yuefang FROM total join employee on total.eid = employee.eid where MONTH(date)=MONTH(CURDATE()) group by employee.eid";
            $result['title'] = $day."报表";
            $result['data']=D()->query($sql);
            header("Access-Control-Allow-Origin:*");
            die(json_encode($result));


        }else{
            $day =date('Y-m',strtotime("-1 day"));
            $sql = "SELECT name,sum(count) as count,sum(greater1) as greater1,sum(greater3) as greater3,sum(yuefang) as yuefang FROM total join employee on total.eid = employee.eid where MONTH(date)=MONTH(CURDATE()) group by employee.eid";
            $result['title'] = $day."报表";
            $result['data']=D()->query($sql);
            header("Access-Control-Allow-Origin:*");
            die(json_encode($result));
        }
    }

    function week(){
        $date1 = I('get.date1');
        $date2 = I('get.date2');
        $sql = "SELECT name,sum(count) as count,sum(greater1) as greater1,sum(greater3) as greater3,sum(other.yuefang) as yuefang ,sum(other.yixiang) as yixiang
            FROM total 
            join employee on total.eid = employee.eid 
            join other on other.eid = total.eid

            where total.date between '{$date1}' and '{$date2}' group by employee.eid";
        $result['title'] = $day."报表";
        $result['data']=D()->query($sql);
        header("Access-Control-Allow-Origin:*");
        die(json_encode($result));
    }

    function weekother(){
        $date1 = I('get.date1');
        $date2 = I('get.date2');
        $sql = "SELECT name,sum(yuefang) as yuefang,sum(yixiang) as yixiang
            FROM other
            join employee on other.eid = employee.eid 
            where other.date between '{$date1}' and '{$date2}' group by employee.eid";
        $result['title'] = $day."报表";
        $result['data']=D()->query($sql);
        header("Access-Control-Allow-Origin:*");
        die(json_encode($result));
    }

}
